{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.3 积分\n",
    "在SymPy中，integrate()函数用于进行积分运算，包括不定积分、定积分、广义积分和无穷积分等。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.3.1 不定积分\n",
    "不定积分是求一个函数的原函数或反导数。使用integrate()函数时，如果不指定积分上下界，则默认计算不定积分。\n",
    "(P184)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "示例：求不定积分 ∫ √(arctan(x)) / ((1+x)√(x)) dx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import Lambda, symbols, integrate, atan, sqrt\n",
    " \n",
    "x = symbols('x')\n",
    "f = Lambda(x, atan(sqrt(x)) / ((1+x)*sqrt(x)))\n",
    "result = integrate(f(x), x)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.3.2 定积分\n",
    "定积分是求一个函数在给定区间上的积分值。使用integrate()函数时，需要指定积分变量和积分上下界。(P224)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import integrate, exp, log\n",
    " \n",
    "x = symbols('x')\n",
    "result = integrate(exp(-x), (x, 0, log(2)))\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另外，还可以使用高斯-勒让德公式来求定积分。gauss_legendre()函数用于生成高斯点和对应的权重，然后利用这些点和权重进行求和，得到积分值。\n",
    "\n",
    "示例：使用高斯-勒让德公式求定积分 ∫_(-1)2) dx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy.integrals.quadrature import gauss_legendre\n",
    "from sympy import Lambda, sqrt\n",
    " \n",
    "n_point = 10\n",
    "xi, wi = gauss_legendre(n_point, 5)\n",
    " \n",
    "# 线性变换将积分区间从[-1, 1]变换到[a, b]\n",
    "a, b = -1, 1\n",
    "t = symbols('t')\n",
    "x = (b - a)/2*t + (a + b)/2\n",
    "f = Lambda(t, sqrt(16 + 6*(x.subs(t, t)) - (x.subs(t, t))**2))\n",
    " \n",
    "gauss_sum = 0\n",
    "for i in range(n_point):\n",
    "    gauss_sum += wi[i] * f(xi[i])\n",
    " \n",
    "# 由于a和b是-1和1，所以不需要再乘以(b-a)/2\n",
    "print(gauss_sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.7 偏导数\n",
    "多元函数偏导与全微分(第65页)\n",
    "\n",
    "例1：设函数z=(x,y)由方程z=e*(2x-3y)+2y确定求3(∂z∂x+∂z∂y)|x=3,y=2的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, exp, diff, lambdify\n",
    "\n",
    "# 定义变量\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义函数\n",
    "f = exp(2*x - 3*y) + 2*y\n",
    "\n",
    "# 计算偏导数\n",
    "df_dx = f.diff(x)\n",
    "df_dy = f.diff(y)\n",
    "\n",
    "# 计算指定点的偏导数值并求和\n",
    "value = 3 * (df_dx.subs({x: 3, y: 2}) + df_dy.subs({x: 3, y: 2}))\n",
    "print(value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求函数\n",
    "z=cos√(x2+y2)的偏导数∂z∂x,∂z∂y,∂2z∂x∂y,全微分dz以及dz|x=1,y=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "&part;z/&part;x: -x*sin(sqrt(x**2 + y**2))/sqrt(x**2 + y**2)\n",
      "&part;z/&part;y: -y*sin(sqrt(x**2 + y**2))/sqrt(x**2 + y**2)\n",
      "&part;z/&part;x at (1, 2): -sqrt(5)*sin(sqrt(5))/5\n",
      "&part;z/&part;y at (1, 2): -2*sqrt(5)*sin(sqrt(5))/5\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, cos, sqrt, diff\n",
    "\n",
    "# 定义变量\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义函数\n",
    "f = cos(sqrt(x**2 + y**2))\n",
    "\n",
    "# 计算偏导数\n",
    "df_dx = f.diff(x)\n",
    "df_dy = f.diff(y)\n",
    "\n",
    "# 输出偏导数\n",
    "print(\"&part;z/&part;x:\", df_dx)\n",
    "print(\"&part;z/&part;y:\", df_dy)\n",
    "\n",
    "# 计算指定点的偏导数值\n",
    "df_dx_value = df_dx.subs({x: 1, y: 2})\n",
    "df_dy_value = df_dy.subs({x: 1, y: 2})\n",
    "\n",
    "# 输出指定点的偏导数值\n",
    "print(\"&part;z/&part;x at (1, 2):\", df_dx_value)\n",
    "print(\"&part;z/&part;y at (1, 2):\", df_dy_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多元函数极值与最值（第111页）\n",
    "\n",
    "例：求函数\n",
    "f(x,y)=x2+y2\n",
    "的极值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{x: 0, y: 0}\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, diff, solve\n",
    "\n",
    "# 定义变量\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义函数\n",
    "f = x**2 + y**2\n",
    "\n",
    "# 求偏导数并解方程组\n",
    "critical_points = solve([f.diff(x), f.diff(y)], (x, y))\n",
    "print(critical_points)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.8 重积分\n",
    "\n",
    "4.8.1 重积分的计算（第135页）\n",
    "\n",
    "例1：计算积分∫20dx∫2xe−y2dy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-(1 - exp(4))*exp(-4)/2\n"
     ]
    }
   ],
   "source": [
    "from sympy import integrate, exp\n",
    "\n",
    "# 计算重积分\n",
    "result = integrate(integrate(exp(-y**2), (y, x, 2)), (x, 0, 2)).simplify()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例：计算三重积分\n",
    "∭Ωzdxdydz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/24\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate\n",
    "\n",
    "x, y, z = symbols('x y z')\n",
    "\n",
    "# 定义积分\n",
    "inner_integral = integrate(z, (z, 0, 1-x-y))\n",
    "middle_integral = integrate(inner_integral, (y, 0, 1-x))\n",
    "outer_integral = integrate(middle_integral, (x, 0, 1))\n",
    "\n",
    "print(outer_integral)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.8.2 重积分的应用\n",
    "示例 1: 求曲面 z=x²+y² 和 z=2−x²−y² 围成的体积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6*pi\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate, pi, sqrt\n",
    "\n",
    "x, y, r, theta = symbols('x y r theta')\n",
    "\n",
    "# 定义积分\n",
    "inner_integral = integrate(6*r - 3*r**3, (r, 0, sqrt(2)))\n",
    "volume_integral = integrate(inner_integral, (theta, 0, 2*pi))\n",
    "\n",
    "print(volume_integral)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "示例 2: 求 ∬D (x² + y) dxdy，其中 D 是由抛物线 y=x² 和 x=y² 所围平面闭区域"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "33/140\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate\n",
    "\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义积分\n",
    "inner_integral = integrate(x**2 + y, (y, x**2, sqrt(x)))\n",
    "outer_integral = integrate(inner_integral, (x, 0, 1))\n",
    "\n",
    "print(outer_integral)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "曲线积分\n",
    "示例 1: 设 L 为 {x = e^t + 1, y = e^t - 1} 从 t=0 到 log(2) 的一段弧，求曲线积分 ∫_L xdx + ydy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate, exp, ln\n",
    "\n",
    "t = symbols('t')\n",
    "x = exp(t) + 1\n",
    "y = exp(t) - 1\n",
    "\n",
    "dx = x.diff(t)\n",
    "dy = y.diff(t)\n",
    "\n",
    "integral = integrate(x * dx + y * dy, (t, 0, ln(2)))\n",
    "\n",
    "print(integral)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "曲面积分\n",
    "示例 1: 计算 ∬_Σ x²dydz + y²dzdx + zdxdy，其中 Σ 是旋转抛物面 z=1−x²−y²(z≥0) 的上侧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi/3\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate, cos, sin, pi\n",
    "\n",
    "r, theta = symbols('r theta')\n",
    "\n",
    "# 内层积分\n",
    "inner_integral = integrate((2*r*cos(theta) + 2*r*sin(theta) + 1)*(1 - r**2)*r, (z, 0, 1-r**2))\n",
    "\n",
    "# 中间层积分\n",
    "middle_integral = integrate(inner_integral, (r, 0, 1))\n",
    "\n",
    "# 外层积分\n",
    "outer_integral = integrate(middle_integral, (theta, 0, 2*pi))\n",
    "\n",
    "print(outer_integral)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意：在实际应用中，如果积分区间不是[-1, 1]，则需要进行线性变换。上面的代码虽然包含了线性变换的部分，但在这个特定例子中并不需要，因为积分区间已经是[-1, 1]。\n",
    "\n",
    "4.3.3 广义积分\n",
    "广义积分（也称为反常积分）是积分区间或函数本身存在某种“反常”情况的积分，如积分区间包含无穷点，或被积函数在积分区间内存在无穷间断点等。求广义积分的方法与求定积分的方法相同，只是结果可能是无穷大。\n",
    "\n",
    "4.3.4 无穷积分\n",
    "无穷积分是积分区间包含无穷点的积分。在SymPy中，可以使用oo（无穷大）作为积分上限或下限来求无穷积分。\n",
    "\n",
    "示例：计算积分 ∫_0^∞ e^(-x^2) dx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import integrate, exp, oo\n",
    " \n",
    "x = symbols('x')\n",
    "result = integrate(exp(-x**2), (x, 0, oo))\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.3.5 瑕积分\n",
    "瑕积分是被积函数在积分区间内存在无穷间断点的积分。判断瑕积分的敛散性也是积分运算的一个重要方面。\n",
    "\n",
    "示例：判断广义积分 ∫_02 的敛散性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import integrate\n",
    " \n",
    "x = symbols('x')\n",
    "try:\n",
    "    result = integrate(1/(1-x)**2, (x, 0, 2))\n",
    "    print(\"积分收敛，结果为：\", result)\n",
    "except ValueError:\n",
    "    print(\"积分发散\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意：在实际应用中，如果积分发散，integrate()函数可能会抛出ValueError异常。因此，可以使用try-except块来捕获这个异常，并判断积分是否发散。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
